var t = require("../../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: {
        language: "",
        showcarrfootBol: !1,
        showcarrfoot_ipxBol: !1,
        firstshow: !1,
        loginshow: !1,
        cartList_none: !1,
        integral: "积分",
        selectBol: !0,
        cartDelete: !1,
        eachCheckList: [],
        eachCartsList: [],
        allCartsList: [],
        all_carts_id: [],
        eachCheckShop: [],
        cart_ids: [],
        cartList: [],
        failureCartList: [],
        total_goods_price: 0,
        total_amount: 0,
        point_mall_total: 0,
        total_discount_amount: 0,
        discount_amount_items: [],
        showCouponDetail: !1,
        isShowList: !1,
        checkAll: !1,
        rentIndex: -1,
        showChoose: !1,
        radioChoose: "",
        dispatch_types: [],
        chooseType: "",
        recommendInfo: [],
        recommendPage: 1,
        recommendLast_page: 1,
        recommendCurrent_page: 1,
        showMore: !1,
        sharin_is_open: !1
    },
    onLoad: function(t) {
        var a = wx.getStorageSync("yz_basic_info");
        this.setData({
            vip_show: !!a.globalParameter.vip_show && a.globalParameter.vip_show
        }), console.log(this.data.vip_show);
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        }), wx.setNavigationBarTitle({
            title: this.data.language.cart["购物车"]
        });
        var a = wx.getStorageSync("integral");
        a && this.setData({
            integral: a
        });
    },
    onShow: function() {
        this.firstlogin() || (this._datainit(), this._getData()), this.designRecommend();
    },
    designRecommend: function() {
        var t = this, e = a.getNetAddresss("cart.list.recommend-goods");
        a._getNetWork({
            url: e,
            data: {
                page: this.data.recommendPage
            },
            success: function(a) {
                var e = a.data;
                1 === e.result ? t.setData({
                    recommendInfo: e.data.data,
                    recommendCurrent_page: e.data.current_page,
                    recommendLast_page: e.data.last_page
                }) : t.setData({
                    recommendInfo: !1,
                    recommendPage: 1,
                    recommendLast_page: 1,
                    recommendCurrent_page: 1
                });
            }
        });
    },
    designRecommendMore: function() {
        var t = this, e = a.getNetAddresss("cart.list.recommend-goods");
        a._getNetWork({
            url: e,
            data: {
                page: this.data.recommendPage
            },
            success: function(a) {
                var e = a.data;
                1 === e.result && t.setData({
                    recommendInfo: t.data.recommendInfo.concat(e.data.data),
                    recommendCurrent_page: e.data.current_page,
                    recommendLast_page: e.data.last_page
                });
            }
        });
    },
    goRecommend: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + a
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.recommendCurrent_page >= this.data.recommendLast_page ? console.log("没有更多了") : (this.setData({
            recommendPage: this.data.recommendPage + 1
        }), this.designRecommendMore());
    },
    onShareAppMessage: function() {},
    onShareTimeline: function() {
        return {
            query: "?id=1"
        };
    },
    showcarrfoot_ipx: function(t) {
        var a = t.detail;
        this.setData({
            showcarrfoot_ipxBol: a
        });
    },
    showcarrfoot: function(t) {
        var a = t.detail;
        this.setData({
            showcarrfootBol: a
        });
    },
    _datainit: function() {
        this.setData({
            selectBol: !0,
            cartDelete: !1,
            eachCheckList: [],
            eachCartsList: [],
            allCartsList: [],
            all_carts_id: [],
            eachCheckShop: [],
            cart_ids: [],
            cartList: [],
            failureCartList: [],
            total_goods_price: 0,
            total_amount: 0,
            point_mall_total: 0,
            total_discount_amount: 0,
            discount_amount_items: [],
            showCouponDetail: !1,
            isShowList: !1,
            checkAll: !1,
            rentIndex: -1,
            showChoose: !1,
            radioChoose: "",
            dispatch_types: [],
            chooseType: ""
        });
    },
    clearFailureCart: function() {
        var t = this;
        wx.showModal({
            title: "提示",
            content: "确定清空失效商品吗？",
            success: function(e) {
                if (e.confirm) {
                    var s = [];
                    t.data.failureCartList.forEach(function(t) {
                        s.push(t.id);
                    });
                    var i = a.getNetAddresss("cart.list.del-failure-cart");
                    a._postNetWork({
                        url: i,
                        data: {
                            cart_ids: s
                        },
                        success: function(e) {
                            1 === e.data.result && (t.setData({
                                failureCartList: []
                            }), a.tips("清空成功"));
                        },
                        fail: function(t) {
                            console.log(t);
                        }
                    });
                } else e.cancel && console.log("用户点击取消");
            }
        });
    },
    _getData: function(t) {
        var e = this, s = {
            cart_ids: this.data.cart_ids.join(",")
        };
        "delete" === t && (s.basic_info = 1);
        var i = a.getNetAddresss("cart.list.index");
        a._postNetWork({
            url: i,
            data: s,
            success: function(s) {
                var i = s.data;
                1 === i.result ? (e.setData({
                    sharin_is_open: i.data.sharin_is_open,
                    total_discount_amount: i.data.total_discount_amount,
                    discount_amount_items: i.data.discount_amount_items,
                    total_amount: Number(i.data.total_amount),
                    point_mall_total: Number(i.data.point_mall_total),
                    total_goods_price: i.data.total_goods_price,
                    cartList: i.data.list,
                    failureCartList: i.data.invalid_cart || []
                }), t ? "delete" === t && e._allCheckListInit(e.data.cartList, t) : e._allCheckListInit(e.data.cartList), 
                e.setData({
                    firstshow: !0,
                    cartList_none: !(i.data.list.length <= 0)
                })) : a.tips(i.msg);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    _allCheckListInit: function(t, a) {
        var e = this;
        this.setData({
            cart_ids: [],
            eachCartsList: [],
            eachCheckList: [],
            eachCheckShop: []
        }), "delete" == a && t.length <= 0 && this.setData({
            cartDelete: !1
        }), t.length && function() {
            for (var a = [], s = [], i = 0; i < t.length; i++) e.data.eachCartsList[i] = t[i].carts, 
            e.data.eachCheckList.push([]), e.data.eachCheckShop.push({
                check: !1,
                code: t[i].code,
                is_alone: t[i].is_alone
            }), "lease-toy" === t[i].code && (e.data.rentIndex = i), a[i] = t[i].carts.map(function(t) {
                return s.push(t.cart_id), t.cart_id.toString();
            });
            e.setData({
                eachCartsList: e.data.eachCartsList,
                eachCheckList: e.data.eachCheckList,
                eachCheckShop: e.data.eachCheckShop,
                rentIndex: e.data.rentIndex,
                allCartsList: a,
                all_carts_id: s,
                isShowList: !0
            });
        }(), this.setData({
            isShowList: !0
        });
    },
    selectBolfun: function(t) {
        this.data.selectBol = "1" === t;
    },
    allSelect: function(a) {
        var e = a.detail;
        if (this.setData({
            checkAll: e
        }), this.selectBolfun("1"), this.data.selectBol) {
            if (this.data.checkAll) if (1 === this.data.allCartsList.length) this.setData(t({
                chooseType: "rent",
                eachCheckList: JSON.parse(JSON.stringify(this.data.allCartsList))
            }, "eachCheckShop[0].check", !0)); else if (this.data.cartDelete) {
                for (var s = JSON.parse(JSON.stringify(this.data.allCartsList)), i = 0; i < s.length; i++) this.data.eachCheckShop[i].check = !0;
                this.setData({
                    eachCheckList: JSON.parse(JSON.stringify(this.data.allCartsList)),
                    eachCheckShop: this.data.eachCheckShop
                });
            } else {
                this.data.chooseType = "buy", this.data.eachCheckList = [];
                for (var o = JSON.parse(JSON.stringify(this.data.allCartsList)), c = 0; c < o.length; c++) this.data.eachCheckShop[c].is_alone ? (this.data.eachCheckShop[c].check = !1, 
                this.data.eachCheckList.push([])) : (this.data.eachCheckShop[c].check = !0, this.data.eachCheckList.push(this.data.allCartsList[c]));
                this.setData({
                    chooseType: "buy",
                    eachCheckList: this.data.eachCheckList,
                    eachCheckShop: this.data.eachCheckShop
                });
            } else {
                this.data.chooseType = "", this.data.eachCheckList = [];
                for (var h = 0; h < this.data.allCartsList.length; h++) this.data.eachCheckList.push([]), 
                this.data.eachCheckShop[h].check = !1;
                this.setData({
                    chooseType: "",
                    eachCheckList: this.data.eachCheckList,
                    eachCheckShop: this.data.eachCheckShop
                });
            }
            this._countCartView();
        }
    },
    allSelectHandle: function(t) {
        var a = t.detail, e = t.currentTarget.dataset.c_index;
        this.selectBolfun("0"), this.data.selectBol || (this.data.eachCheckShop[e].check = a.length === this.data.eachCartsList[e].length, 
        this.data.eachCheckList[e] = a, this.setData({
            eachCheckShop: this.data.eachCheckShop,
            eachCheckList: this.data.eachCheckList
        }), this._countCartView());
    },
    shopChange: function(t) {
        var a = this, e = t.detail, s = t.currentTarget.dataset.c_index;
        this.data.cartList[s].disable || (this.selectBolfun("0"), this.data.selectBol || (e ? (this.data.eachCheckShop[s].check = !0, 
        this.data.eachCheckList[s].splice(0, this.data.eachCheckList[s].length), this.data.eachCartsList[s].map(function(t) {
            a.data.eachCheckList[s].push(t.cart_id.toString());
        })) : (this.data.eachCheckShop[s].check = !1, this.data.eachCheckList[s].splice(0, this.data.eachCheckList[s].length)), 
        this.setData({
            eachCheckShop: this.data.eachCheckShop,
            eachCheckList: this.data.eachCheckList
        }), this._countCartView()));
    },
    onCartDelete: function() {
        this.data.eachCheckList = [];
        for (var t = 0; t < this.data.eachCartsList.length; t++) this.data.eachCheckList.push([]), 
        this.data.cartList[t].disable = !1, this.data.eachCheckShop[t].check = !1;
        this.selectBolfun("1"), this.setData({
            cartDelete: !this.data.cartDelete,
            chooseType: "",
            checkAll: !1,
            eachCheckList: this.data.eachCheckList,
            cart_ids: [],
            total_amount: 0,
            point_mall_total: 0,
            total_goods_price: 0,
            total_discount_amount: 0,
            cartList: this.data.cartList,
            eachCheckShop: this.data.eachCheckShop
        });
    },
    deleteGoods: function() {
        var t = this;
        if (0 != this.data.cart_ids.length) {
            var e = a.getNetAddresss("cart.list.destroy");
            e += "&ids=" + this.data.cart_ids.join(","), a._getNetWork({
                url: e,
                success: function(a) {
                    var e = a.data;
                    1 == e.result ? t._getData("delete") : wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        } else wx.showToast({
            icon: "none",
            title: "请选择删除商品",
            duration: 1500
        });
    },
    checkGoods: function(t) {
        var e = this;
        if (0 != this.data.cart_ids.length) if (this.data.cart_ids.length > 50) wx.showToast({
            icon: "none",
            title: "最多一次性只能下单50样商品",
            duration: 1500
        }); else {
            var s = a.getNetAddresss("memberCart.checkout");
            s += "&cart_ids=" + this.data.cart_ids.join(","), a._getNetWork({
                url: s,
                success: function(t) {
                    var a = t.data;
                    1 == a.result ? 1 == a.data.need_choose ? e.setData({
                        dispatch_types: a.data.dispatch_types,
                        showChoose: !0
                    }) : e.submitGoods() : wx.showModal({
                        title: "提示",
                        content: a.msg,
                        showCancel: !1,
                        success: function(t) {}
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        } else wx.showToast({
            icon: "none",
            title: "请选择结算商品",
            duration: 1500
        });
    },
    toBuy: function() {
        this.submitGoods();
    },
    submitGoods: function() {
        var t = [];
        if (!this.data.showChoose || this.data.radioChoose.toString()) {
            if (this.data.showChoose) for (var a = 0; a < this.data.dispatch_types[this.data.radioChoose].member_carts.length; a++) t.push(this.data.dispatch_types[this.data.radioChoose].member_carts[a].id); else t = this.data.cart_ids;
            "rent" === this.data.chooseType ? wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=rentCartBuy&cart_ids=" + JSON.stringify(t)
            }) : "buy" === this.data.chooseType && wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-1&cart_ids=" + JSON.stringify(t)
            });
        } else wx.showToast({
            icon: "none",
            title: "请先选择配送方式",
            duration: 1500
        });
    },
    toGoodsInfo: function(t) {
        var a = t.currentTarget.dataset.option_id, e = t.currentTarget.dataset.goodsid;
        a ? wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + e + "&coid=" + a
        }) : wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + e
        });
    },
    toShop: function(t) {
        var a = t.currentTarget.dataset.item.merchant_id;
        a > 0 && wx.navigateTo({
            url: "/packageA/member/supplier/SupplierShop/SupplierShop?id=" + a
        });
    },
    toSearch: function(t) {
        var a = t.currentTarget.dataset.item.merchant_id;
        a > 0 ? wx.navigateTo({
            url: "/packageA/member/supplier/SupplierShop/SupplierShop?id=" + a
        }) : wx.navigateTo({
            url: "/packageB/member/category/search_v2/search_v2?plugin_id=1"
        });
    },
    _changeCount: function(e, s, i, o) {
        var c = this;
        if (e < 0) return wx.showToast({
            icon: "none",
            title: "商品数量不能为负数",
            duration: 1500
        }), void this.setData(t({}, "cartList[".concat(o, "].carts[").concat(i, "].total"), 1));
        var h = a.getNetAddresss("cart.list.updateNumV2");
        h += "&id=" + s, h += "&num=" + e, a._getNetWork({
            url: h,
            success: function(t) {
                var a = t.data;
                c._countCartView(), 0 == a.result && wx.showModal({
                    title: "提示",
                    content: a.msg,
                    showCancel: !1,
                    success: function(t) {}
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    numberLeft: function(a) {
        var e = a.currentTarget.dataset.index, s = a.currentTarget.dataset.c_index;
        this.data.cartList[s].carts[e].total <= 1 ? wx.showToast({
            icon: "none",
            title: "商品数量不能为负数或零",
            duration: 1500
        }) : (this.setData(t({}, "cartList[".concat(s, "].carts[").concat(e, "].total"), Number(this.data.cartList[s].carts[e].total) - 1)), 
        this._changeCount(this.data.cartList[s].carts[e].total, this.data.cartList[s].carts[e].cart_id, e, s));
    },
    numberRight: function(a) {
        var e = a.currentTarget.dataset.index, s = a.currentTarget.dataset.c_index;
        this.setData(t({}, "cartList[".concat(s, "].carts[").concat(e, "].total"), Number(this.data.cartList[s].carts[e].total) + 1)), 
        this._changeCount(this.data.cartList[s].carts[e].total, this.data.cartList[s].carts[e].cart_id, e, s);
    },
    blurCount: function(t) {
        var a = t.currentTarget.dataset.goodtotal, e = t.currentTarget.dataset.cart_id, s = t.currentTarget.dataset.index, i = t.currentTarget.dataset.c_index;
        this._changeCount(a, e, s, i);
    },
    goodinpbtn: function(a) {
        var e = a.currentTarget.dataset.index, s = a.currentTarget.dataset.c_index, i = a.detail.value;
        this.setData(t({}, "cartList[".concat(s, "].carts[").concat(e, "].total"), i));
    },
    _countCartView: function() {
        for (var t = [], a = 0; a < this.data.eachCheckList.length; a++) this.data.eachCheckList[a].map(function(a) {
            t.push(a);
        });
        this.setData({
            cart_ids: t
        }), this.checkRent(), this.checkChooseAll(), this.data.cartDelete || this._getData("count");
    },
    checkChooseAll: function() {
        if (this.data.cartDelete) this.data.all_carts_id.length === this.data.cart_ids.length ? this.data.checkAll = !0 : this.data.checkAll = !1; else {
            var t = 0;
            if (this.data.allCartsList.length > 1) for (var a = 0; a < this.data.allCartsList.length; a++) this.data.eachCheckShop[a].is_alone && (t += this.data.allCartsList[a].length);
            this.data.all_carts_id.length - t === this.data.cart_ids.length ? this.data.checkAll = !0 : this.data.checkAll = !1;
        }
        this.setData({
            checkAll: this.data.checkAll
        });
    },
    checkRent: function() {
        this.data.cart_ids.length > 0 ? this.data.rentIndex > -1 && this.data.eachCheckList[this.data.rentIndex].length > 0 ? this.data.chooseType = "rent" : this.data.chooseType = "buy" : this.data.chooseType = "", 
        this.setData({
            chooseType: this.data.chooseType
        });
    },
    closeChoose: function() {
        this.setData({
            showChoose: !1
        });
    },
    typeChange: function(t) {
        this.setData({
            radioChoose: Number(t.detail)
        });
    },
    openCoupon: function() {
        this.setData({
            showCouponDetail: !0
        });
    },
    closeCoupon: function() {
        this.setData({
            showCouponDetail: !1
        });
    },
    gomember: function() {
        wx.reLaunch({
            url: "/packageG/member_v2/member_v2"
        });
    },
    gohome: function() {
        wx.reLaunch({
            url: "/packageG/index/index"
        });
    },
    gotologin: function() {
        a.seyzredirect(), wx.navigateTo({
            url: "/pages/login/login"
        });
    },
    firstlogin: function() {
        var t = !1;
        try {
            wx.getStorageSync("wx_token") ? (this.setData({
                loginshow: !1
            }), t = !1) : (this.setData({
                loginshow: !0
            }), t = !0);
        } catch (t) {}
        return t;
    },
    toShowMore: function() {
        this.setData({
            showMore: !this.data.showMore
        });
    },
    shareCartGood: function() {
        wx.navigateTo({
            url: "/packageF/cart_share/cart_share"
        });
    }
});